//
//package com.avicit.zipkinkafkaesserver.controller;
//
//import com.avicit.zipkinkafkaesserver.entity.ZipkinInterfaceAverageDuration;
//import com.avicit.zipkinkafkaesserver.entity.ZipkinInterfaceCount;
//import com.avicit.zipkinkafkaesserver.service.SparkService;
//import com.avicit.zipkinkafkaesserver.service.ZipkinInterfaceAverageDurationService;
//import com.avicit.zipkinkafkaesserver.service.ZipkinInterfaceCountService;
//import com.avicit.zipkinkafkaesserver.utils.TimeUtils;
//import com.avicit.zipkinkafkaesserver.vo.InterfaceCountVo;
//import com.avicit.zipkinkafkaesserver.vo.RealtimeAvgDurationChartDataKvVo;
//import com.avicit.zipkinkafkaesserver.vo.RealtimeAvgDurationChartDataVo;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.ui.Model;
//import org.springframework.util.StringUtils;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.ResponseBody;
//import scala.Tuple2;
//
//import java.io.IOException;
//import java.util.*;
//import java.util.stream.Collectors;
//
//@Controller
//@RequestMapping("/zipkin")
//public class ZipKinController {
//
//    @Autowired
//    private ZipkinInterfaceCountService zipkinInterfaceCountService;
//
//    //接口平均耗时
//    @Autowired
//    private ZipkinInterfaceAverageDurationService zipkinInterfaceAverageDurationService;
//
//    @Autowired
//    private SparkService sparkService;
//
//    @RequestMapping("/index")
//    public String index() {
//        return "/zipkin/index";
//    }
//
//    /**
//     * 历史数据查询
//     * top 100
//     * 默认查询 1 天的
//     * @param queryDateFrom 开始时间
//     * @param queryDateTo  结束时间
//     * @param serverName  服务名
//     * @param interfacePath  接口
//     * @param model
//     * @return
//     */
//    @RequestMapping("/history/interfaceCount")
//    public String historyInterfaceCount(
//                                 @RequestParam String queryDateFrom,
//                                 @RequestParam String queryDateTo,
//                                 @RequestParam(required = false) String serverName,
//                                 @RequestParam(required = false) String interfacePath,
//                                 Model model) {
//        //如果queryDateFrom == null 就取当前日期的前一天作为开始时间
//        queryDateFrom = queryDateFrom == null ? TimeUtils.backFor(1) : queryDateFrom;
//        queryDateTo = queryDateTo == null ? TimeUtils.nowDateString() : queryDateTo;
//        List<ZipkinInterfaceCount> interfaceCountList = zipkinInterfaceCountService.interfaceCountsv2(queryDateFrom, queryDateTo, serverName, interfacePath);
//        model.addAttribute("countVos", interfaceCountList);
//        model.addAttribute("queryDateFrom", queryDateFrom);
//        model.addAttribute("queryDateTo", queryDateTo);
//        model.addAttribute("serverName", serverName);
//        model.addAttribute("interfacePath", interfacePath);
//        return "/zipkin/historyInterfaceCount";
//    }
//
//    /**
//     * 实时数据查询
//     * top 100
//     * 默认查询 1 天的
//     * queryDate 日期 2019-06-11
//     * startTime 开始时间 09:00:00
//     * endTime 结束时间  09:15:00
//     * @param serverName  服务名
//     * @param interfaceName  接口名
//     * @param model
//     * @return
//     */
//
//    @RequestMapping("/interfaceCount")
//    public String interfaceCount(@RequestParam String queryDate,
//                                 @RequestParam String startTime,
//                                 @RequestParam String endTime,
//                                 @RequestParam(required = false) String serverName,
//                                 @RequestParam(required = false) String interfaceName,
//                                 Model model) throws IOException {
//
//
//        long currentTimeMillis = System.currentTimeMillis();
//        queryDate = queryDate == null ? TimeUtils.nowDateString() : queryDate;
//
//        long startTimeSecond = 0;
//        long endTimesecond = 0;
//
//        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)&& !startTime.equals(endTime)) {
//            if (startTime.length()<8 ) {
//                startTime = 0+startTime;
//            }
//            if (endTime.length()<8 ) {
//                endTime = 0+endTime;
//            }
//            // 日期 + 时间 2019-06-11 09:00:00
//            StringBuilder sbStartTime = new StringBuilder(queryDate).append(" ").append(startTime);
//            StringBuilder sbEndTime = new StringBuilder(queryDate).append(" ").append(endTime);
//            startTimeSecond = TimeUtils.dateTimeToMilliSecond(sbStartTime.toString(), TimeUtils.DatePattern.ALLTIME);
//            endTimesecond = TimeUtils.dateTimeToMilliSecond(sbEndTime.toString(), TimeUtils.DatePattern.ALLTIME);
//        } else {
//            endTimesecond = currentTimeMillis;//为空 取当前时间
//            //为空取当前时间前 5分钟
//            startTimeSecond = currentTimeMillis - 5*60*1000;
//        }
//        List<Tuple2<String, Integer>> tuple2List = sparkService.interfaceCountHasStatus(startTimeSecond,endTimesecond,100, serverName,interfaceName);
//
//        Map<String,Integer> okMap = new HashMap<>();
//        Map<String,Integer> errorMap = new HashMap<>();
//        Set<String>pathSet = new HashSet<>();
//        tuple2List.stream().forEach(item->{
//            //server-a@GET@/getInfo@ok
//            String key = item._1;
//            //server-a@GET@/getInfo
//            String path = key.substring(0,key.lastIndexOf("@"));
//            // ok
//            String status = key.substring(key.lastIndexOf("@")+1);
//            //统计正确的和错误的
//            if ("ok".equals(status)) {okMap.put(path,item._2);}
//            if ("error".equals(status)){errorMap.put(path,item._2);}
//            pathSet.add(path);
//        });
//        List<InterfaceCountVo> countVos = new ArrayList<>();
//
//        //从两个map 集合去取数据
//        for (String key : pathSet) {
//            //server-a@GET@/getInfo
//            String[] split = key.split("@");
//            Integer okCount = okMap.get(key);
//            Integer errorCount = errorMap.get(key);
//            okCount  = okCount==null?0: okCount;
//            errorCount = errorCount ==null?0: errorCount;
//            InterfaceCountVo countVo = InterfaceCountVo.builder()
//                    .errorCount(errorCount)
//                    .serverName(split[0])
//                    .interfaceName(split[2])
//                    .count(okCount + errorCount).build();
//            countVos.add(countVo);
//        }
//        model.addAttribute("queryDate", queryDate);
//        model.addAttribute("startTime", startTime);
//        model.addAttribute("endTime", endTime);
//        model.addAttribute("serverName", serverName);
//        model.addAttribute("interfaceName", interfaceName);
//        model.addAttribute("countVos",countVos);
//        return "/zipkin/interfaceCount";
//    }
//
//    /**
//     * 历史平均耗时
//     * @param queryDateFrom
//     * @param queryDateTo
//     * @param serverName
//     * @param interfacePath
//     * @param model
//     * @return
//     */
//    @Autowired
//    private ZipkinInterfaceAverageDurationService service;
//    @RequestMapping("/history/interfaceAvgDuration")
//    public String historyInterfaceAvgDuration(
//                                       @RequestParam String queryDateFrom,
//                                       @RequestParam String queryDateTo,
//                                       @RequestParam(required = false) String serverName,
//                                       @RequestParam(required = false) String interfacePath,
//                                       Model model
//    ) {
//        //如果queryDateFrom == null 就取当前日期的前一天作为开始时间
//        queryDateFrom = queryDateFrom == null ? TimeUtils.backFor(1) : queryDateFrom;
//        queryDateTo = queryDateTo == null ? TimeUtils.nowDateString() : queryDateTo;
//        List<ZipkinInterfaceAverageDuration> interfaceAverageDurationList = service.interfaceAvgDuration(queryDateFrom,queryDateTo,serverName,interfacePath);
//        model.addAttribute("countVos",interfaceAverageDurationList);
//        model.addAttribute("queryDateFrom", queryDateFrom);
//        model.addAttribute("queryDateTo", queryDateTo);
//        model.addAttribute("serverName", serverName);
//        model.addAttribute("interfacePath", interfacePath);
//        return "/zipkin/historyInterfaceAvgDuration";
//    }
//
//
//    /**
//     * 实时接口平均耗时
//     * @return
//     * @throws IOException
//     */
//    @RequestMapping("/interfaceAvgDuration")
//    public String interfaceDurationCount(@RequestParam String queryDate,
//                                         @RequestParam String startTime,
//                                         @RequestParam String endTime,
//                                         @RequestParam(required = false) String serverName,
//                                         @RequestParam(required = false) String interfaceName,
//                                         Model model
//    ) throws IOException {
//        long currentTimeMillis = System.currentTimeMillis();
//        queryDate = queryDate == null ? TimeUtils.nowDateString() : queryDate;
//
//        long startTimeSecond = 0;
//        long endTimesecond = 0;
//
//        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)&& !startTime.equals(endTime)) {
//            if (startTime.length()<8 ) {
//                startTime = 0+startTime;
//            }
//            if (endTime.length()<8 ) {
//                endTime = 0+endTime;
//            }
//            // 日期 + 时间 2019-06-11 09:00:00
//            StringBuilder sbStartTime = new StringBuilder(queryDate).append(" ").append(startTime);
//            StringBuilder sbEndTime = new StringBuilder(queryDate).append(" ").append(endTime);
//            startTimeSecond = TimeUtils.dateTimeToMilliSecond(sbStartTime.toString(), TimeUtils.DatePattern.ALLTIME);
//            endTimesecond = TimeUtils.dateTimeToMilliSecond(sbEndTime.toString(), TimeUtils.DatePattern.ALLTIME);
//        } else {
//            endTimesecond = currentTimeMillis;//为空 取当前时间
//            //为空取当前时间前 5分钟
//            startTimeSecond = currentTimeMillis - 5*60*1000;
//        }
//
//        List<Tuple2<String, Double>> tuple2s = (List<Tuple2<String, Double>>)sparkService.interfaceDurationCountNoIp(startTimeSecond,endTimesecond,100,serverName,interfaceName).get("countResult");
//        List<InterfaceCountVo> countVos = tuple2s.stream().map(tuple2 -> {
//            String[] split = tuple2._1.split("@");
//            double value = tuple2._2;
//            return InterfaceCountVo.builder()
////                    .ip(split[0])
//                    .serverName(split[0])
//                    .interfaceName(split[2])
//                    .duration((int) value)
//                    .build();
//        }).collect(Collectors.toList());
//        model.addAttribute("countVos",countVos);
//        model.addAttribute("queryDate", queryDate);
//        model.addAttribute("startTime", startTime);
//        model.addAttribute("endTime", endTime);
//        model.addAttribute("serverName", serverName);
//        model.addAttribute("interfaceName", interfaceName);
//        return "/zipkin/interfaceAvgDuration";
//    }
//
//    /**
//     * 实时接口耗时列表, 接口调用明细
//     * @param min
//     * @param max
//     * @return
//     */
//    @RequestMapping("/interfaceDurationCountList")
//    public String interfaceDurationCountList(@RequestParam String queryDate,
//                                             @RequestParam String startTime,
//                                             @RequestParam String endTime,
//                                             @RequestParam String serverName,
//                                             @RequestParam String interfaceName,
//                                             Model model
//    ) throws IOException {
//        long currentTimeMillis = System.currentTimeMillis();
//        queryDate = queryDate == null ? TimeUtils.nowDateString() : queryDate;
//
//        long startTimeSecond = 0;
//        long endTimesecond = 0;
//
//        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)&& !startTime.equals(endTime)) {
//            if (startTime.length()<8 ) {
//                startTime = 0+startTime;
//            }
//            if (endTime.length()<8 ) {
//                endTime = 0+endTime;
//            }
//            // 日期 + 时间 2019-06-11 09:00:00
//            StringBuilder sbStartTime = new StringBuilder(queryDate).append(" ").append(startTime);
//            StringBuilder sbEndTime = new StringBuilder(queryDate).append(" ").append(endTime);
//            startTimeSecond = TimeUtils.dateTimeToMilliSecond(sbStartTime.toString(), TimeUtils.DatePattern.ALLTIME);
//            endTimesecond = TimeUtils.dateTimeToMilliSecond(sbEndTime.toString(), TimeUtils.DatePattern.ALLTIME);
//        } else {
//            endTimesecond = currentTimeMillis;//为空 取当前时间
//            //为空取当前时间前 5分钟
//            startTimeSecond = currentTimeMillis - 5*60*1000;
//        }
//        List<Tuple2<String, Double>> tuple2s = sparkService.interfaceDurationCountList(startTimeSecond, endTimesecond,100,serverName,interfaceName);
//        List<InterfaceCountVo> countVos = tuple2s.stream().map(tuple2 -> {
//            String[] split = tuple2._1.split("@");
//            double value = tuple2._2;
//            return InterfaceCountVo.builder()
//                    .ip(split[0])
//                    .serverName(split[1])
//                    .interfaceName(split[3])
//                    .status(split[4])
//                    .dateTime(TimeUtils.timestamToDatetime(Long.valueOf(split[5])))
//                    .duration((int) value)
//                    .build();
//        }).collect(Collectors.toList());
//        model.addAttribute("countVos",countVos);
//        model.addAttribute("queryDate", queryDate);
//        model.addAttribute("startTime", startTime);
//        model.addAttribute("endTime", endTime);
//        model.addAttribute("serverName", serverName);
//        model.addAttribute("interfaceName", interfaceName);
//        return "/zipkin/interfaceDurationCountList";
//    }
//
//    /**
//     * 耗时5分钟曲线图
//     * @return
//     */
//    @RequestMapping("/realtimeAvgDuration")
//    public String realtimeAvgDuration() {
//       return "/zipkin/realtimeAvgDuration";
//    }
//
//    /**
//     * 图标数据查询
//     * @param queryDateFrom 开始日期
//     * @param queryDateTo 结束日期
//     * @param serverName 服务名
//     * @param interfacePath 服务接口
//     * @param model
//     * @return
//     */
//    @RequestMapping("/realtimeAvgDurationChartData")
//    @ResponseBody
//    public List<RealtimeAvgDurationChartDataVo> getRealtimeAvgDurationChartDataKv(
//            @RequestParam String queryDateFrom,
//            @RequestParam String queryDateTo,
//            @RequestParam String serverName,
//            @RequestParam String interfacePath,
//            Model model
//    ) throws IOException {
//        List<RealtimeAvgDurationChartDataVo> result = zipkinInterfaceAverageDurationService.getRealtimeAvgDurationChartData(queryDateFrom,queryDateTo,serverName,interfacePath);
//        return result;
//    }
//
//    /**
//     * 图标数据查询
//     * @param queryDateFrom 开始日期
//     * @param queryDateTo 结束日期
//     * @param serverName 服务名
//     * @param interfacePath 服务接口
//     * @param model
//     * @return
//     */
//    @RequestMapping("/realtimeAvgDurationChartDataKv")
//    @ResponseBody
//    public List<RealtimeAvgDurationChartDataKvVo> getRealtimeAvgDurationChartData(
//            @RequestParam String queryDateFrom,
//            @RequestParam String queryDateTo,
//            @RequestParam  String serverName,
//            @RequestParam String interfacePath,
//            Model model
//    ) throws IOException {
//        List<RealtimeAvgDurationChartDataVo> result = zipkinInterfaceAverageDurationService.getRealtimeAvgDurationChartData(queryDateFrom,queryDateTo,serverName,interfacePath);
//        List<RealtimeAvgDurationChartDataKvVo> collect = result.stream().map(item ->
//                RealtimeAvgDurationChartDataKvVo.builder().reqs(item.getReqs()/1000).time(item.getQueryStartTime()/1000).build()).collect(Collectors.toList());
//        return collect;
//    }
//}
